package com.kamloo.stg.data.db;

import java.util.ArrayList;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class NodeDAO {
	
	private SQLiteDatabase m_database;
	private MySQLiteHelper m_dbHelper;
	public static final String TABLE_NAME 	 			= "node";
	public static final String COLUMN_ID 		 		= "_id";
	public static final String COLUMN_FK_DIRECTION_ID	= "fk_directionId";
	public static final String COLUMN_FK_STOP_ID		= "fk_stopId";
	private String[] m_allColumns = { COLUMN_ID, COLUMN_FK_DIRECTION_ID, COLUMN_FK_STOP_ID };
	
	public static final String TABLE_CREATE = 
			"CREATE TABLE " + 
		    TABLE_NAME + "(" + 
		    COLUMN_ID + " INTEGER PRIMARY KEY, " + 
		    COLUMN_FK_DIRECTION_ID + " INTEGER REFERENCES " + DirectionDAO.TABLE_NAME + "(" + DirectionDAO.COLUMN_ID + ") " +
		    		"ON UPDATE CASCADE ON DELETE CASCADE, " + 
		    COLUMN_FK_STOP_ID + " INTEGER REFERENCES " + StopDAO.TABLE_NAME + "(" + StopDAO.COLUMN_ID + ") " +
		    		"ON UPDATE CASCADE ON DELETE CASCADE);";
	
	public NodeDAO(Context context) {
		m_dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		m_database = m_dbHelper.getWritableDatabase();
	}

	public void close() {
		m_dbHelper.close();
	}
	
	
	public void add(Node node) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ID, node.getId());
		values.put(COLUMN_FK_DIRECTION_ID, node.getFk_directionId());
		values.put(COLUMN_FK_STOP_ID, node.getFk_stopId());
		m_database.insert(TABLE_NAME, null, values);
	}
	
	public void delete(int id) {
		System.out.println("Node deleted with id: " + id);
		m_database.delete(TABLE_NAME, COLUMN_ID + " = " + id, null);
	}
	
	public Node select(int id) {
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, COLUMN_ID + " = " + id, 
									  null, null, null, null);
		
		if (cur != null)
			cur.moveToFirst();
		
		Node newNode = cursorToNode(cur);
		cur.close();
		return newNode;
	}
	
	public ArrayList<Node> selectFromDirection(int directionId) 
	{
		ArrayList<Node> nodes = new ArrayList<Node>();
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, 
									  COLUMN_FK_DIRECTION_ID + " = " + directionId, 
									  null, null, null, null);
		
		if (cur != null) {
			cur.moveToFirst();
			while (!cur.isAfterLast()) {
				nodes.add(cursorToNode(cur));
				cur.moveToNext();
			}
		}
		
		cur.close();
		return nodes;
	}
	
	public void update(Node node) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_FK_DIRECTION_ID, node.getFk_directionId());
		values.put(COLUMN_FK_STOP_ID, node.getFk_stopId());
		m_database.update(TABLE_NAME, values, COLUMN_ID + " = " + node.getId(), null);
	}	
	
	private Node cursorToNode(Cursor cursor) {
		return new Node(cursor.getInt(0),
						cursor.getInt(1),
						cursor.getInt(2));
	}
}
